﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace EWDT_CDS_Hub.App_Code
{
    public class Diploma
    {
        public String Diploma_id { get; set; }
        public String Diploma_name { get; set; }
        public String School_id { get; set; }

        public static List<Diploma> GetDiploma()
        {
            List<Diploma> results = new List<Diploma>();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM Diploma";
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    Diploma d = new Diploma();
                    d.Diploma_id= (String)dr["diploma_id"];
                    d.Diploma_name = (String)dr["diploma_name"];
                    d.School_id = (String)dr["school_id"];

                    results.Add(d);
                }
            }
            catch (SqlException e)
            {
            }
            finally
            {
                conn.Close();
            }

            return results;
        }

        public static int InsertDiploma(Diploma d)
        {
            int rowsInserted = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO Diploma (diploma_id, diploma_name, school_id) VALUES (@id, @name, @sid)";
                comm.Parameters.AddWithValue("@id", d.Diploma_id);
                comm.Parameters.AddWithValue("@name", d.Diploma_name);
                comm.Parameters.AddWithValue("@sid", d.School_id);
                rowsInserted = comm.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {

            }
            finally
            {
                conn.Close();
            }

            return rowsInserted;
        }

        public static int UpdateDiploma(Diploma d)
        {
            int rowsUpdated = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "UPDATE Diploma SET diploma_name = @name, school_id = @schid WHERE diploma_id = @id";
                comm.Parameters.AddWithValue("@id", d.Diploma_id);
                comm.Parameters.AddWithValue("@name", d.Diploma_name);
                comm.Parameters.AddWithValue("@schid", d.School_id);
                rowsUpdated = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return rowsUpdated;
        }

        public static Diploma RetrieveDiploma(String id)
        {
            Diploma d = null;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM Diploma WHERE diploma_id = @id";
                comm.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    d = new Diploma();
                    d.Diploma_id = (String)dr["diploma_id"];
                    d.Diploma_name = (String)dr["diploma_name"];
                    d.School_id = (String)dr["school_id"];
                }

                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return d;
        }
    }
}